package com.netease.qa.biz.service.impl;

import com.nestease.qa.dao.api.*;
import com.nestease.qa.dao.entity.Project;
import com.nestease.qa.dao.entity.Team;
import com.nestease.qa.dao.entity.TestCase;
import com.nestease.qa.dao.params.ProjectQueryParams;
import com.nestease.qa.dao.params.TestCaseQueryParams;
import com.netease.qa.biz.dto.TeamDTO;
import com.netease.qa.biz.enums.ProjectStatus;
import com.netease.qa.biz.enums.TeamStatus;
import com.netease.qa.biz.enums.TestCaseStatus;
import com.netease.qa.biz.enums.UserTeamStatus;
import com.netease.qa.biz.service.TeamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zoudongdong
 * @date 2018/2/14
 */
@Service
public class TeamServiceImpl implements TeamService {

    @Resource
    private TeamDAO teamDAO;
    @Resource
    private UserDAO userDAO;
    @Resource
    private UserTeamDAO userTeamDAO;
    @Resource
    private TestCaseDAO testCaseDAO;
    @Resource
    private ProjectDAO projectDAO;

    @Transactional
    public void addTeam(TeamDTO teamDTO) {
        Team team = new Team();
        team.setTeamId(teamDTO.getTeamId());
        team.setTeamName(teamDTO.getTeamName());
        team.setCreator(teamDTO.getCreator());
        team.setLastEditor(teamDTO.getCreator());
        team.setStatus(TeamStatus.Valid.getValue());
        teamDAO.addTeam(team);

        userTeamDAO.joinUserTeam(teamDTO.getCreator(), teamDTO.getTeamId());
    }

    public List<TeamDTO> getAllTeam() {
        List<Team> teamList = teamDAO.getALLTeamList(TeamStatus.Valid.getValue());
        List<TeamDTO> result = new ArrayList<TeamDTO>();
        TeamDTO teamDTO;
        for (Team team : teamList) {
            teamDTO = new TeamDTO();
            teamDTO.setTeamId(team.getTeamId());
            teamDTO.setTeamName(team.getTeamName());

            //获取该团队对应的操作者
            StringBuilder sb = new StringBuilder();
            List<String> userIdList = userTeamDAO.getJoinedUserIdList(team.getTeamId());
            for (String userId : userIdList
                    ) {
                String operatorName = userDAO.getUserNameByUserId(userId);
                sb.append(operatorName + ",");
            }
            String sbStr = sb.toString();
            teamDTO.setOperatorName(sbStr.substring(0, sbStr.length() - 1));
            result.add(teamDTO);
        }
        return result;
    }

    public List<TeamDTO> getJoinedTeamList(String userId) {
        List<Integer> joinedTeamList = userTeamDAO.getJoinedTeamIdList(userId);
        List<TeamDTO> result = new ArrayList<TeamDTO>();
        TeamDTO teamDTO;
        for (Integer joinedTeam : joinedTeamList
                ) {
            Team team = teamDAO.getTeamDetail(joinedTeam);
            teamDTO = new TeamDTO();
            teamDTO.setTeamId(team.getTeamId());
            teamDTO.setTeamName(team.getTeamName());
            result.add(teamDTO);
        }
        return result;
    }

    public int getNextTeamId() {
        int maxTeamId = teamDAO.getMaxTeamId();
        return ++maxTeamId;
    }

    public void joinTeam(String joinUserId, int teamId) {
        int count = userTeamDAO.ifOnceJoined(joinUserId, teamId);
        if (count > 0) {
            //用户之前加入过这个组
            userTeamDAO.updateUserTeam(joinUserId, teamId, UserTeamStatus.Valid.getValue());
        } else {
            userTeamDAO.joinUserTeam(joinUserId, teamId);
        }
    }

    public List<String> getJoinedUserIdList(int teamId) {
        List<String> joinedUserIdList = userTeamDAO.getJoinedUserIdList(teamId);
        return joinedUserIdList;
    }

    public void leaveTeam(String leaveUserId, int teamId) {
        userTeamDAO.leaveUserTeam(leaveUserId, teamId);
    }

    public void editTeam(TeamDTO teamDTO, String lastEditor) {
        Team team = new Team();
        team.setTeamId(teamDTO.getTeamId());
        team.setTeamName(teamDTO.getTeamName());
        team.setLastEditor(lastEditor);

        teamDAO.editTeam(team);
    }

    public TeamDTO getTeamDetail(int teamId) {
        Team team = teamDAO.getTeamDetail(teamId);

        TeamDTO teamDTO = new TeamDTO();
        teamDTO.setTeamId(team.getTeamId());
        teamDTO.setTeamName(team.getTeamName());
        teamDTO.setCreator(team.getCreator());
        return teamDTO;
    }

    public List<Integer> getTeamIdList() {
        List<Integer> teamIdList = teamDAO.getTeamIdList();
        return teamIdList;
    }

    @Transactional
    public void deleteTeam(int teamId, String userId) {
        //查询该团队下是否有有效的测试用例
        TestCaseQueryParams testCaseQueryParams = new TestCaseQueryParams();
        testCaseQueryParams.setTeamId(teamId);
        testCaseQueryParams.setStatus(TestCaseStatus.Valid.getValue());
        List<TestCase> testCaseDTOList = testCaseDAO.getTestCaseInCondition(testCaseQueryParams);
        if (testCaseDTOList != null) {
            //删除测试用例
            for (TestCase testCase : testCaseDTOList
                    ) {
                testCase.setLastEditor(userId);
                testCaseDAO.deleteTestCase(testCase);
            }
        }

        //查询该团队下是否有有效的项目
        ProjectQueryParams projectQueryParams = new ProjectQueryParams();
        projectQueryParams.setTeamId(teamId);
        projectQueryParams.setStatus(ProjectStatus.Valid.getValue());
        List<Project> projectDTOList = projectDAO.getProjectListInCondition(projectQueryParams);
        if (projectDTOList != null) {
            //删除项目
            for (Project project : projectDTOList
                    ) {
                project.setLastEditor(userId);
                projectDAO.deleteProject(project);
            }
        }

        //最后删除团队
        Team team = teamDAO.getTeamDetail(teamId);
        team.setLastEditor(userId);
        teamDAO.deleteTeam(team);
    }

    public boolean getTeamByName(String teamName) {
        int count = teamDAO.getTeamByName(teamName);
        return count > 0 ? true : false;
    }

    public String validateBeforeDelete(int teamId) {
        //查询该团队下是否还有状态为有效的项目
        ProjectQueryParams params = new ProjectQueryParams();
        params.setTeamId(teamId);
        params.setStatus(ProjectStatus.Valid.getValue());
        List<Project> projectDTOList = projectDAO.getProjectListInCondition(params);

        //查询该团队-->项目下是否有有效的测试用例
        TestCaseQueryParams testCaseQueryParams = new TestCaseQueryParams();
        testCaseQueryParams.setTeamId(teamId);
        testCaseQueryParams.setStatus(TestCaseStatus.Valid.getValue());
        List<TestCase> testCaseDTOList = testCaseDAO.getTestCaseInCondition(testCaseQueryParams);

        if (projectDTOList.size() == 0 && testCaseDTOList.size() == 0) {
            return "验证通过";
        } else if (projectDTOList.size() == 0) {
            return "该团队下还有有效的测试用例,删除后会将所有数据置为无效,确认删除吗?";
        } else if (testCaseDTOList.size() == 0) {
            return "该团队下还有有效的项目,删除后会将所有数据置为无效,确认删除吗?";
        }
        return "该团队下还有有效的项目和测试用例,删除后会将所有数据置为无效,确认删除吗?";
    }
}
